package com.hukun.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //可变参数 在形参中只能放在最后
        //可变参数的实质就是数组
        //但是比数组更加灵活 可变长度和不用定义数组对象
      int [] array = {1,2,3,4};
        int [] array2 = {0};
        //System.out.println(  sum(array));

        System.out.println( sum2(array2));
      //  System.out.println(sum1(1,2,3,4,5,76,78));

        System.out.println(sum1(1));
    }

   //overload
    public static int sum(int b ,int ...a) {
        int result = 0;
        for (int i :
                a) {
            result += i;

        }
        return result;
    }




    public static int sum2(int[] a) {
        int result = 0;
        for (int i :
                a) {
            result += i;

        }
        return result;
    }


    public static int sum1 (int ... a)
    {
        int result = 0;
        for (int i :
                a) {
            result += i;

        }
        return result;
    }

    private static void test3() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //先创建class 对象
        // test2();
        // test1("com.hukun.clazz.BMWCar");
        Class clazz = Class.forName("com.hukun.clazz.BMWCar2");
        //1如何找到对应的方法
        //Method method = clazz.getDeclaredMethod("getPatent");
        Method method = clazz.getDeclaredMethod("drive", String.class);
        //设置私有方法的访问权限
        method.setAccessible(true);
        //如何通过class对象生成person对象
        Object o = clazz.newInstance();
//        //如何调用方法
        System.out.println(method.invoke(o, "tom"));
        //System.out.println( method.invoke(o));
    }

    private static void test2(String className) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class clazz = Class.forName(className);
        //1如何找到对应的方法
        Method method = clazz.getDeclaredMethod("intruduce", String.class, int.class);
        //如何通过class对象生成person对象
        Object o = clazz.newInstance();
        //如何调用方法
        System.out.println(method.invoke(o, "tom", 20));

        Method method1 = clazz.getDeclaredMethod("setName", String.class);
        method1.invoke(o, "jarry");
        Method method2 = clazz.getDeclaredMethod("getName");
        System.out.println(method2.invoke(o));
    }

    private static void test1(String className) throws ClassNotFoundException {
        //BMWCar bmwCar = new BMWCar("A");
        //获取Class对象
        Class clazz = Class.forName(className);
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor c :
                constructors) {
            System.out.println(c);

        }
        System.out.println(">>>>>>>>>>>>>>>>");
        Field[] declaredFields = clazz.getDeclaredFields();

        for (Field c :
                declaredFields) {
            System.out.println(c);

        }
        System.out.println(">>>>>>>>>>>>>>>>");
        // getMethods   得到所有Object类的方法
        //getDeclaredMethods得到本类的所有方法
        Method[] declaredMethods = clazz.getDeclaredMethods();

        for (Method c :
                declaredMethods) {
            System.out.println(c);

        }
    }


}
